package com.spark.colliery.module.agency.fragment

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.google.gson.Gson
import com.spark.colliery.R
import com.spark.common.BaseFragment
import com.spark.common.Const
import com.spark.common.ext.report
import com.spark.common.ext.wrapIO
import com.spark.common.net.Http
import com.spark.common.net.toJsonBody
import com.spark.colliery.module.agency.TrackTaskInfoActivity
import kotlinx.android.synthetic.main.frag_agency_todo.*

/**
 * 任务追踪列表
 */

class TaskTrackFragment : BaseFragment() {
    private lateinit var trackTaskAdapter: TrackTaskAdapter

    companion object {
        fun newInstance(): TaskTrackFragment {
            val fragment = TaskTrackFragment()
            val args = Bundle()

            fragment.arguments = args
            return fragment
        }
    }

    private var pageNum = 1
    private var pageSize = 10

    override fun contentResId(): Int {
        return R.layout.frag_agency_todo
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        trackTaskAdapter = TrackTaskAdapter(R.layout.item_track_task)
        recy_agency_tasks.apply {
            adapter = trackTaskAdapter
            layoutManager = LinearLayoutManager(context)
        }
        refresh_layout.apply {
            setOnRefreshListener {
                pageNum = 1
                fetckTrackTaks(pageNum)
            }
            setOnLoadmoreListener {
                pageNum++
                fetckTrackTaks(pageNum)
            }
        }

        trackTaskAdapter.setOnItemClickListener { adapter, view, position ->
            val data = trackTaskAdapter.data[position]
            //parentFragment?.parentFragment?.parentFragment?.let { it as MainFragment }?.starz(TrackTaskInfoFragment.newInstance("${data.id}"))
            //start(TrackTaskInfoFragment.newInstance("${data.id}"), Intent.FLAG_ACTIVITY_SINGLE_TOP)
            activity?.apply { TrackTaskInfoActivity.actionStart(this, "${data.id}") }
        }
    }

    override fun onLazyInitView(savedInstanceState: Bundle?) {
        super.onLazyInitView(savedInstanceState)
        fetckTrackTaks(pageNum)
    }

    private fun fetckTrackTaks(page: Int) {
        val param = mutableMapOf(
                Pair(Const.qr_pageNum, "$page"),
                Pair(Const.qr_pageSize, "$pageSize")
        )
        activity?.apply {
            Http.request()
                    .rxPost("${Const.base_url}${Const.act_track_tasks}", param.toJsonBody())
                    .wrapIO()
                    .subscribe(
                            {
                                Gson().fromJson(it, com.spark.colliery.module.agency.model.TrackTaskListModel::class.java)?.apply {
                                    if (code == "200") {
                                        if (page == 1)
                                            trackTaskAdapter.data.clear()
                                        trackTaskAdapter.addData(this.resultMaps)
                                    }
                                }
                            },
                            {
                                it.printStackTrace()
                                report(it)
                                if (page == 1) refresh_layout.finishRefresh() else refresh_layout.finishLoadmore()
                            },
                            {
                                if (page == 1) refresh_layout.finishRefresh() else refresh_layout.finishLoadmore()
                            },
                            {
                                dispose = it
                            }
                    )
        }
    }
}

class TrackTaskAdapter(layoutRes: Int) : BaseQuickAdapter<com.spark.colliery.module.agency.model.TrackTaskListModel.ResultMap, BaseViewHolder>(layoutRes) {
    override fun convert(helper: BaseViewHolder?, item: com.spark.colliery.module.agency.model.TrackTaskListModel.ResultMap?) {
        if (item == null) return
        helper?.apply {
            setText(R.id.tv_agency_task_title, item.title)
            setImageResource(R.id.iv_state, when (item.state) {
                "1" -> R.drawable.ic_task_waiting
                "2" -> R.drawable.ic_task_one_step
                "3" -> R.drawable.ic_task_complete
                "4" -> R.drawable.ic_task_to_chk
                else -> R.drawable.ic_task_waiting
            })
            setText(R.id.tv_agency_task_content, item.description)
            setText(R.id.tv_task_info, item.description)
        }
    }

}
